phpDocumentor Web Commons
[ class tree: Web Commons ] [ index: Web Commons ] [ all elements ]

Source for file environment.php

Documentation is available at environment.php

  1. <?php
  2. /**
  3.  * This file groups classes pertaining to execution environment.
  4.  * 
  5.  * @author Antoine d'Otreppe de Bouvette <a.dotreppe@aspyct.org>
  6.  * @license http://www.opensource.org/licenses/mit-license.php
  7.  * @version 0.1dev
  8.  */
  9.  
  10. /**
  11.  * Offers read-only properties related to application context.
  12.  * 
  13.  * This class offers read-only properties named after the existing createXxx
  14.  * methods. A "createConfig" method would offer a "config" attribute.
  15.  * These "createXxx" methods will be called only once, so that each represent
  16.  * a kind of singleton.
  17.  * This is useful for providing a global access to main application resources
  18.  * like configuration, request objects and such.
  19.  * Some methods are provided as a default behaviour.
  20.  * 
  21.  * @since 0.1
  22.  */
  23. abstract class AbstractContext {
  24.     /**
  25.      * Contains the previously created properties.
  26.      * 
  27.      * @var array 
  28.      */
  29.     private $props array();
  30.     
  31.     /**
  32.      * Returns the object/scalar/array/whatever created by the method
  33.      * "createXxx" where "Xxx" = ucfirst($attr). If no such method exists, a
  34.      * ContextException is thrown.
  35.      * 
  36.      * @param string $attr 
  37.      * @return mixed 
  38.      * @since 0.1
  39.      */
  40.     public function __get($attr{
  41.         if (!array_key_exists($attr$this->props)) {
  42.             $method array($this'create' ucfirst($attr));
  43.             
  44.             if (is_callable($method)) {
  45.                 $this->props[$attrcall_user_func($method);
  46.             }
  47.             else {
  48.                 throw new ContextException(
  49.                     'No attribute "' $attr '" in context.');
  50.             }
  51.         }
  52.         
  53.         return $this->props[$attr];
  54.     }
  55. }
  56.  
  57. /**
  58.  * Provides access to request parameters.
  59.  * @since 0.1
  60.  */
  61. interface Request {
  62.     /**
  63.      * Returns the request method, uppercased.
  64.      * @return string 
  65.      * @since 0.1
  66.      */
  67.     function method();
  68.     
  69.     /**
  70.      * Returns the GET parameter corresponding to $key, or $default if this key
  71.      * is not defined.
  72.      * 
  73.      * @param string $key 
  74.      * @param mixed $default 
  75.      * @return mixed 
  76.      * @since 0.1
  77.      */
  78.     function get($key$default=Null);
  79.     
  80.     /**
  81.      * Returns the POST parameter corresponding to $key, or $default if this key
  82.      * is not defined.
  83.      * 
  84.      * @param string $key 
  85.      * @param mixed $default 
  86.      * @return mixed 
  87.      * @since 0.1
  88.      */
  89.     function post($key$default=Null);
  90. }
  91.  
  92. /**
  93.  * A request that fits a standard web environment.
  94.  * See {@link Request} for method details.
  95.  * 
  96.  * @since 0.1
  97.  */
  98. class HTTPRequest implements Request {
  99.     /**
  100.      * (non-PHPdoc)
  101.      * @see WebCommons/Request::method()
  102.      */
  103.     public function method({
  104.         return strtoupper($_SERVER['REQUEST_METHOD']);
  105.     }
  106.     
  107.     /**
  108.      * (non-PHPdoc)
  109.      * @see WebCommons/Request::post()
  110.      */
  111.     public function post($key$default=Null{
  112.         return array_get_default($_POST$key$default);
  113.     }
  114.     
  115.     /**
  116.      * (non-PHPdoc)
  117.      * @see WebCommons/Request::get()
  118.      */
  119.     public function get($key$default=Null{
  120.         return array_get_default($_GET$key$default);
  121.     }
  122. }
  123.  
  124. /**
  125.  * Exception thrown when a requested attribute is missing in a Context object.
  126.  * 
  127.  * @since 0.1
  128.  */
  129. class ContextException extends Exception {}

Documentation generated on Fri, 16 Jul 2010 00:48:39 +0200 by phpDocumentor 1.4.3